<?php
/**
* This file serves auto-listing to the script that lease it.
* auto-listing.php will refer to this script.
*/

# Refer to auto-listing project directory
$parent = dirname(__FILE__);
require_once($parent.'/inc.config.php');

# A leased script should define a directory to look for
if(defined('LOOKUP_DIR') && is_dir(LOOKUP_DIR))
{
	$lookup_dir = LOOKUP_DIR;
}

$linker = new linker();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Applications</title>
<style type="text/css">
<!--
<?php
# images and other resource may be lost, due to change in URLs
readfile($parent.'/css/list.css');
?>

/** image icons do not work. So, color the links */
.inactive a
{
	color:#FF0000;
}
.inactive a:visited
{
	color:#CC6633;
}
-->
</style>
</head>
<body>
<div class="wrapper">
  <h1>Folders:</h1>
  <ul>
<?php
$folders_counter = 0;
if ($handle = opendir($lookup_dir))
{
	while (false !== ($file = readdir($handle)))
	{
		$physical_file = $lookup_dir.'/'.$file;
		if(!is_dir($physical_file) || $file=='.' || $file=='..')
		{
			continue;
		}
		
		++$folders_counter;
		echo $linker->regular_directory($physical_file, $file);
	}
	closedir($handle);
}
else
{
	echo "<li>Unable to read the lookup directory.</li>";
}

if(!$folders_counter)
{
	echo "<li>No folders yet.</li>";
}
?>
  </ul>
  <div class="clear"></div>
  <h1>PHP Files:</h1>
  <ul>
<?php
# Avoid myself; as a leased auto listing index file.
$myself = basename($_SERVER['SCRIPT_FILENAME']);

$files_counter = 0;
if ($handle = opendir($lookup_dir))
{
	while (false !== ($file = readdir($handle)))
	{
		$physical_file = $lookup_dir.'/'.$file;
		if(!is_file($physical_file) || $file=='index.php' || $file==$myself)
		{
			continue;
		}
		
		# Use PHP only filter
		if(!preg_match('/\.php$/', $file))
		{
			continue;
		}
		
		echo $linker->regular_file($file);
		
		++$files_counter;
	}
	closedir($handle);
}
else
{
	echo "<li>Unable to read the lookup directory.</li>";
}

if(!$files_counter)
{
	echo "<li>No files yet.</li>";
}
?>
  </ul>
  <div class="clear"></div>
</div>
</body>
</html>